package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.LoginCredentials;
import model.Staff;


import dao.StaffDAO;

import dao.mysql.StaffDAOImpl;

/**
 * Servlet implementation class LoadStaffServlet
 */
@WebServlet("/LoadStaffServlet")
public class LoadStaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadStaffServlet() {
        super();
        
      
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		loadStaff(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		loadStaff(request,response);
	}
	private void loadStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
		HttpSession session=request.getSession(false);
		
		if(session!=null){
		StaffDAO staff=new StaffDAOImpl();
		LoginCredentials loginfo=new LoginCredentials();
		
		ArrayList<Staff> allstaff=null;
		
		try {
			//alllogs=loginfo.findAllLoginCredentials();
			allstaff=staff.combine(loginfo);
			request.setAttribute("allstaff",allstaff);
			RequestDispatcher rd=request.getRequestDispatcher("ModifyStaff.jsp");
			rd.forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		}
		else{
			response.sendRedirect("Login.jsp");
		}
	}
	
	

}
